$(function() {
    try {
        $("#listing-form").submit(function() {
            try {
                var longTermLease = $('#listing-leaseLongTerm').is(':checked');
                var shortTermLease = $("#listing-leaseShortTerm").is(':checked');
                
                if (!longTermLease && !shortTermLease) {
                    alert('You have to pick a Rental Term for your property. Is it for Long Term, for Short Term or Both?');  
                    return false;
                }
                         
                if (longTermLease) {
                    var monthRate = $("#listing-longMonthRate").val();
                    if (!monthRate) {
                        alert('Monthly rate is required for long term lease.');
                        return false;
                    }  
                }
                
                if (shortTermLease) {
                    var dayRate = $("#listing-shortDayRate").val();
                    if (!dayRate) {
                        alert('Daily rate is required for short term lease.');
                        return false;
                    }  
                }                
                                
                var propertyType = $("#listing-propertyType").val();
                if (propertyType === '0') {
                    alert('Property type is required.');
                    return false;
                }

                var structure = $("#listing-structure").val();
                if (!structure) {
                    alert('Building/Village name is required.');
                    return false;
                }
                
                var city = $("#listing-city").val();
                if (!city) {
                    alert('City is required.');
                    return false;
                }
                
               
                /*
                var street = $("#listing-street").val();
                if (!street) {
                    alert('Street is required.');
                    return false;
                }
                              
                var zip = $("#listing-zip").val();
                if (zip === '0000') {
                    alert('Zip code is required.');
                    return false;
                }
               
                var sqArea = $("#listing-sqArea").val();
                if (!sqArea) {
                    alert('Square area is required.');
                    return false;
                }
                */
               
                var furnishing = $("#listing-furnishing").val();
                if (furnishing === '0') {
                    alert('Furnishing is required.');
                    return false;
                }

                /*
                var availabilityDate = $("#listing-availabilityDate").val();
                if (!availabilityDate) {
                    alert('Availability date is required.');
                    return false;
                }
                */
               
                var title = $("#listing-title").val();
                if (!title) {
                    alert('Title of ad is required.');
                    return false;
                }

            }
            catch (e) {
                //alert(e);
                return false;
            }
        });

        $("#listing-availabilityDate").datepicker();
        $("#listing-postDate").datepicker();
        $("#listing-expiryDate").datepicker();
        $("#listing-dateCreated").datepicker();
        
        $("#listing-structure").blur( function() {
            buildDefaultTilte();
        });
        $("#listing-propertyType").on("change", function() {            
            buildDefaultTilte();
        });
        $("#listing-numBedrooms").on("change", function() {            
            buildDefaultTilte();
        });
    } 
    catch (e) {
        alert(e);
    }
}); 

function buildDefaultTilte() {
    var bedrooms = "";
    var type = "";
    var struct = "";
    
    if ($("#listing-structure").val()) {
        struct = "at " + $("#listing-structure").val();
    }
    if ($("#listing-propertyType").val() != "0") {
        var propertyType = $("#listing-propertyType").val();
        if (propertyType == "1") {
            type = "House";
        }
        else if (propertyType == "2") {
            type = "Condo";
        } 
        else if (propertyType == "4") {
            type = "Townhouse";
        } 
    }
    if ($("#listing-numBedrooms").val()) {
        var numBedrooms = $("#listing-numBedrooms").val();
        if (numBedrooms == 0) {
            bedrooms = "Studio";
        }
        else {
            bedrooms = numBedrooms + " Bedroom";
        }
    }
    
    var title = bedrooms + " " + type + " " + struct;
    $("#listing-title").val(title);
}